angular.module('Controller')

.controller('WFParamsSettingCtrl', ($scope, NgTableParams, commonUtil, commonConstant, menuManage, $rootScope, $uibModal, $http) ->
    initSearchData = ()->
        $scope.search = {
            name: undefined
        }
    initSearchData()
    $scope.enabledList = commonConstant.enabledList(true);
    $scope.tableParams = new NgTableParams(
        {page: 1, count: commonConstant.pageTableCount},
        {
            counts: [5, 10, 20],
            getData: ($defer, params)->
                $scope.doQuery($defer,params)
                return ;
        }
    )
    $scope.doQuery = ($defer, params)->
        requestData = {
            dicIndex: $rootScope.pageParams.id,
            page: params.page() - 1,
            size: params.count(),
            nameLike: $scope.search.name
        }
        getParam = commonUtil.transferGetParam(requestData)
        $http.get('/dictionary'+getParam)
        .success (resp)->
            if resp.code == 0
                console.log(resp.message);
                $scope.tableData = resp.message.content;
                params.total(resp.message.totalElements);
                $defer.resolve($scope.tableData);
                $scope.paramsName = $rootScope.pageParams.name;
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newRole: ->
            data={enabled:1}
            showModal('new',data)
        editRole: (data)->
            theData = commonUtil.cloneObj(data)
            showModal('edit', theData)
        deleteRole: (data)->
            $http.delete('/dictionary/'+data.id)
            .success (resp)->
                console.log(resp.message)
                $scope.queryData()
            .error (err)->
                console.log(err)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
    showModal = (type, data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'WFParamsSettingEditCtrl',
            templateUrl: 'templates/system/parameters/params-setting-modal.html',
            resolve: {
                items: ()-> {type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)